package com.moyang.learn.alibabanacosdiscoveryserver.rest;

import com.moyang.learn.alibabanacosdiscoveryserver.entity.User;
import com.moyang.learn.alibabanacosdiscoveryserver.inter.IUserInter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import static com.alibaba.nacos.client.config.impl.ClientWorker.log;

@RestController
@Slf4j
public class TestRest {

//    @GetMapping("/hello/{name}")
//    public String hello(@PathVariable(name = "name", required = false) String name) {
//        log.debug("传入的参数：{}", name);
//        return "hello , " + name;
//    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello1/{id}")
    public String hello1(@PathVariable(name = "id") String id) {
        log.debug("传入的参数：{}", id);
        String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server2/user/" + id, String.class);
        return result;
    }


    /******************** 使用webClient *****************************************/

    @Autowired
    private WebClient.Builder webClientBuilder;

    @GetMapping("hello2/{id}")
    public Mono<String> hello2(@PathVariable("id") String id) {

        return webClientBuilder.build()
                .get()
                .uri("http://alibaba-nacos-discovery-server2/user/" + id)
                .retrieve()
                .bodyToMono(String.class);
    }

    /******************** 使用Feign *****************************************/

    @Autowired
    private IUserInter userInter;

    @GetMapping("hello3/{id}")
    public User hello3(@PathVariable("id") String id) {
        log.info("hello3 获取到的参数Id={}", id);
        return userInter.getUser(id);
    }


}
